/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.hadoop.action;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableOutputFormat;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2013-8-1 上午09:36:55
 */

public class WordCountHBase {
	static class MapClass extends Mapper<LongWritable, Text, Text, IntWritable> {
		private Text		word	= new Text();
		final IntWritable	count	= new IntWritable(1);
		
		public void map(LongWritable key, Text value, Context context) {
			StringTokenizer itr = new StringTokenizer(value.toString());
			while (itr.hasMoreTokens()) {
				word.set(itr.nextToken());
				try {
					context.write(word, count);
				}
				catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	static class ReduceClass extends Reducer<Text, IntWritable, NullWritable, Put> {
		
		public void reduce(Text key, Iterable<IntWritable> values, Context context) {
			int sum = 0;
			for (IntWritable value : values) {
				sum += value.get();
			}
			try {
				Put put = new Put(Bytes.toBytes(key.toString()));
				put.add(Bytes.toBytes("col_content"), Bytes.toBytes("count"), Bytes.toBytes(String.valueOf(sum)));
				context.write(NullWritable.get(), put);
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void createHBaseTable(String tableName) throws IOException {
		HTableDescriptor htd = new HTableDescriptor(tableName);
		HColumnDescriptor col = new HColumnDescriptor("col_content");
		htd.addFamily(col);
		HBaseConfiguration hconfig = new HBaseConfiguration();
		HBaseAdmin hadmin = new HBaseAdmin(hconfig);
		if (hadmin.tableExists(tableName)) {
			System.out.println("the table " + tableName + " exits,it will be delete before create!");
			hadmin.disableTable(tableName);
			hadmin.deleteTable(tableName);
		}
		System.out.println("create table " + tableName);
		hadmin.createTable(htd);
	}
	
	public static void main(String[] args) throws Exception {
		String tableName = "wordcount";
		Configuration conf = new Configuration();
		conf.set(TableOutputFormat.OUTPUT_TABLE, tableName);
		createHBaseTable(tableName);
		String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
		if (otherArgs.length != 1) {
			System.err.println("Usage: wordcount <in>");
			System.exit(2);
		}
		Job job = new Job(conf, "my word count");
		job.setJarByClass(WordCountHBase.class);
		job.setMapperClass(MapClass.class);
		job.setReducerClass(ReduceClass.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);
		job.setInputFormatClass(TextInputFormat.class);
		job.setOutputFormatClass(TableOutputFormat.class);
		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}
